/**
 * @file acl.cpp
 *
 * Copyright (C) Huawei Technologies Co., Ltd. 2019-2020. All Rights Reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "acl/acl.h"

#include <mutex>

#include "runtime/rt_preload_task.h"
#include "runtime/rt.h"
#include "adx_datadump_server.h"
#include "common/util/error_manager/error_manager.h"
#include "log_inner.h"
#include "toolchain/plog.h"
#include "dump.h"
#include "profiling.h"
#include "error_codes_inner.h"
#include "toolchain/resource_statistics.h"
#include "platform/platform_info.h"
#include "utils/cann_info_utils.h"
#include "json_parser.h"
#include "utils/hash_utils.h"
#include "runtime/acl_rt_impl_base.h"
#include "runtime/init_callback_manager.h"

namespace {
    bool aclFinalizeFlag = false;
    thread_local static std::string aclRecentErrMsg;
    bool isEnableDefaultDevice = false;
    constexpr int32_t INVALID_DEFAULT_DEVICE = -1;
    constexpr int32_t ACL_DEFAULT_DEVICE_DISABLE = 0xFFFFFFFF;
    std::string aclInitJsonHash;
    std::string aclInitJsonPath;
    constexpr const char_t *const kAscendHomeEnvName = "ASCEND_HOME_PATH";
    constexpr const char_t *const kVersionInfoKey = "Version=";
    constexpr const char_t *const kVersionDirInfoKey = "version_dir=";
    constexpr const char_t *const kVersionDirPrefix = "CANN-";
    constexpr const char_t *const kDriverPathKey = "Driver_Install_Path_Param=";
    const std::string kAscendInstallPath = "/etc/ascend_install.info";
    const std::map<aclCANNPackageName, std::string> kMapToPkgName = {
        { ACL_PKG_NAME_CANN, "runtime" },
        { ACL_PKG_NAME_RUNTIME, "runtime" },
        { ACL_PKG_NAME_COMPILER, "compiler" },
        { ACL_PKG_NAME_HCCL, "hccl" },
        { ACL_PKG_NAME_TOOLKIT, "toolkit" },
        { ACL_PKG_NAME_OPP, "opp" },
        { ACL_PKG_NAME_OPP_KERNEL, "opp_kernel" },
        { ACL_PKG_NAME_DRIVER, "driver" },
    };

    aclError GetPlatformInfoWithKey(const std::string &key, int64_t *value)
    {
#ifdef __GNUC__
        const char *socName = aclrtGetSocNameImpl();
        if (socName == nullptr) {
            ACL_LOG_ERROR("Init SocName failed");
            return ACL_ERROR_INTERNAL_ERROR;
        }
        // call after aclInit
        const std::string socVersion(socName);

        // init platform info
        if (fe::PlatformInfoManager::GeInstance().InitializePlatformInfo() != 0U) {
            ACL_LOG_INNER_ERROR("init runtime platform info failed, SocVersion = %s", socVersion.c_str());
            return ACL_ERROR_INTERNAL_ERROR;
        }

        fe::PlatFormInfos platformInfos;
        fe::OptionalInfos optionalInfos;
        if (fe::PlatformInfoManager::GeInstance().GetPlatformInfos(socVersion, platformInfos, optionalInfos) != 0U) {
            ACL_LOG_INNER_ERROR("get platform info failed, SocVersion = %s", socVersion.c_str());
            return ACL_ERROR_INTERNAL_ERROR;
        }
        std::string strVal;
        if (!platformInfos.GetPlatformResWithLock("SoCInfo", key, strVal)) {
            ACL_LOG_CALL_ERROR("get platform result failed, key = %s", key.c_str());
            return ACL_ERROR_INTERNAL_ERROR;
        }

        try {
            *value = std::stoll(strVal);
        } catch (...) {
            ACL_LOG_INNER_ERROR("strVal[%s] can not be converted to digital value", strVal.c_str());
            return ACL_ERROR_INTERNAL_ERROR;
        }
        ACL_LOG_INFO("Successfully get platform info, key = %s, value = %ld", key.c_str(), *value);
#endif
        return ACL_SUCCESS;
    }
}

namespace acl {
void resetAclJsonHash()
{
    aclInitJsonHash.clear();
}

void aclGetMsgCallback(const char_t *msg, uint32_t len)
{
    if (msg == nullptr) {
        return;
    }
    (void)aclRecentErrMsg.assign(msg, static_cast<size_t>(len));
}

int32_t UpdateOpSystemRunCfg(void *cfgAddr, uint32_t cfgLen)
{
    ACL_LOG_INFO("start to execute UpdateOpSystemRunCfg");
    if ((cfgAddr == nullptr) || (static_cast<size_t>(cfgLen) < sizeof(size_t))) {
        ACL_LOG_ERROR("UpdateOpSystemRunCfg failed, invalid config address(null) or invalid length %u.", cfgLen);
        return ACL_ERROR_RT_PARAM_INVALID;
    }

    // get device id
    int32_t devId = 0;
    auto rtErr = rtGetDevice(&devId);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_ERROR("can not get device id, runtime errorCode is %d", rtErr);
        return rtErr;
    }

    uint64_t offset = 0;
    // rts interface
    rtErr = rtGetL2CacheOffset(devId, &offset);
    if (rtErr != RT_ERROR_NONE) {
        if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
            ACL_LOG_WARN("can not get l2 cache offset, feature not support, device id = %d", devId);
        } else {
            ACL_LOG_ERROR("can not get l2 cache offset, runtime errorCode is %d, device id = %d", rtErr, devId);
        }
        return rtErr;
    }

    uint64_t *addr = static_cast<uint64_t *>(cfgAddr);
    *addr = offset;

    ACL_LOG_INFO("execute UpdateOpSystemRunCfg successfully, l2 cache offset is %lu, device id = %d", offset, devId);
    return ACL_RT_SUCCESS;
}

aclError HandleErrorManagerConfig(const char_t *const configPath, error_message::ErrorMsgMode &error_mode) {
    const std::string ACL_ERR_MSG_CONFIG_NAME = "err_msg_mode";
    const std::string INTERNAL_MODE = "\"0\"";
    const std::string PROCESS_MODE = "\"1\"";
    error_mode = error_message::ErrorMsgMode::INTERNAL_MODE;

    if ((configPath != nullptr) && (strlen(configPath) != 0UL)) {
        std::string strConfig = PROCESS_MODE;
        bool found = false;
        auto ret = acl::JsonParser::GetJsonCtxByKey(configPath, strConfig, ACL_ERR_MSG_CONFIG_NAME, found);
        if (ret != ACL_SUCCESS) {
            ACL_LOG_INNER_ERROR("can not parse err_msg config from file[%s], errorCode = %d", configPath, ret);
            return ret;
        }
        if (!found) {
            return ACL_SUCCESS;
        }
        ACL_LOG_INFO("err_msg mode is set [%s].", strConfig.c_str());
        if (strConfig == INTERNAL_MODE) {
            error_mode = error_message::ErrorMsgMode::INTERNAL_MODE;
        } else if (strConfig == PROCESS_MODE) {
            error_mode = error_message::ErrorMsgMode::PROCESS_MODE;
        } else {
            ACL_LOG_INNER_ERROR("err_msg mode config is invalid %s", strConfig.c_str());
            return ACL_ERROR_INVALID_PARAM;
        }
    }
    return ACL_SUCCESS;
}

aclError HandleDefaultDeviceAndStackSize(const char_t *const configPath) {
    size_t stackSize = 0;
    bool stackSizeExist = false;
    auto ret = acl::JsonParser::GetStackSize(configPath, stackSize, stackSizeExist);
    if (ret != ACL_SUCCESS) {
        return ACL_ERROR_FAILURE;
    }
    rtError_t rtErr = RT_ERROR_NONE;
    if (stackSizeExist) {
        rtErr = rtDeviceSetLimit(0, RT_LIMIT_TYPE_STACK_SIZE, static_cast<uint32_t>(stackSize));
        if (rtErr != RT_ERROR_NONE) {
            ACL_LOG_CALL_ERROR("set limit (aicore_stack_size %zu)failed, runtime result = %d.",
                stackSize, static_cast<int32_t>(ret));
            return ACL_GET_ERRCODE_RTS(rtErr);
        }
        ACL_LOG_INFO("get aicore stack size %zu success\n", stackSize);
    }
    int32_t defaultDeviceId = INVALID_DEFAULT_DEVICE;
    ret = acl::JsonParser::GetDefaultDeviceIdFromFile(configPath, defaultDeviceId);
    if (ret != ACL_SUCCESS) {
        return ACL_ERROR_FAILURE;
    }
    if (defaultDeviceId == INVALID_DEFAULT_DEVICE) {
        return ACL_SUCCESS;
    }
    rtErr = rtSetDefaultDeviceId(defaultDeviceId);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("set default device id failed, ret:%d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }
    isEnableDefaultDevice = true;
    ACL_LOG_INFO("set default device %d success\n", defaultDeviceId);
    return ACL_SUCCESS;
}


bool IsEnableAutoUCMemeory()
{
    const char_t *autoUcMemory = nullptr;
    MM_SYS_GET_ENV(MM_ENV_AUTO_USE_UC_MEMORY, autoUcMemory);
    // enable: env does not exist or set to 1
    bool enable = ((autoUcMemory == nullptr) || (strlen(autoUcMemory) == 0UL) || (autoUcMemory[0] == '1'));
    ACL_LOG_INFO("auto-uc-memory is %s.", enable ? "enabled" : "disabled");
    return enable;
}

void GetAllPackageVersion()
{
    for (const auto &pkgName : kMapToPkgName) {
        aclCANNPackageVersion pkgVersion = {};
        auto ret = aclsysGetCANNVersion(pkgName.first, &pkgVersion);
        if (ret == ACL_SUCCESS) {
            ACL_LOG_EVENT("Version of %s package is %s", pkgName.second.c_str(), pkgVersion.version);
        } else {
            ACL_LOG_EVENT("Version of %s package is not found", pkgName.second.c_str());
        }
    }
}
}

aclError aclInit(const char *configPath)
{
    ACL_STAGES_REG(acl::ACL_STAGE_INIT, acl::ACL_STAGE_DEFAULT);
    ACL_LOG_INFO("start to execute aclInit");
    const std::unique_lock<std::recursive_mutex> lk(acl::GetAclInitMutex());

    auto &aclInitRefCount = acl::GetAclInitRefCount();
    if (aclInitRefCount > 0) {
        aclInitRefCount++;
        ACL_LOG_INFO("repeatedly initialized, new aclInitRefCount: %lu", aclInitRefCount);
        return ACL_ERROR_REPEAT_INITIALIZE;
    }

    std::string configStr;
    auto ret = acl::GetStrFromConfigPath(configPath, configStr);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("Get content from configPath failed, ret=%d", ret);
        return ret;
    }
    acl::SetConfigPathStr(configStr);

    // 读取并计算当前文件的哈希值（若文件不存在或无法打开，上面的json_parser中会检验住，此处无须再次判断）
    std::string currentHash;
    acl::hash_utils::CalculateSimpleHash(configPath, configStr, currentHash);
    // 内容不一致
    if (!aclInitJsonHash.empty() && currentHash != aclInitJsonHash) {
        ACL_LOG_ERROR("config content of [%s] differs from the first aclInit config file path: [%s]", configPath, aclInitJsonPath.c_str());
        return ACL_ERROR_INVALID_PARAM;
    }

    error_message::ErrorMsgMode error_mode = error_message::ErrorMsgMode::INTERNAL_MODE;
    ACL_LOG_INFO("call ErrorManager.Initialize");
    ret = acl::HandleErrorManagerConfig(configPath, error_mode);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("[Process][ErrorMsg]process HandleErrorManagerConfig failed, ret=%d", ret);
        return ret;
    }

    int32_t initRet = static_cast<int32_t>(ErrorManager::GetInstance().Init(error_mode));
    if (initRet != 0) {
        ACL_LOG_WARN("can not init errorManager, errorCode = %d", initRet);
    }
    if (DlogReportInitialize() != 0) {
        ACL_LOG_WARN("can not init device's log module");
    }

    // init acl_model
    auto cfgStr = configStr.c_str();
    auto cfgLen = configStr.size();
    ret = acl::InitCallbackManager::GetInstance().NotifyInitCallback(ACL_REG_TYPE_ACL_MODEL,
                                                                     cfgStr, cfgLen);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("call acl_model init callback failed, ret:%d", ret);
        return ret;
    }

    if ((configPath != nullptr) && (strlen(configPath) != 0UL)) {
        // config dump
        ACL_LOG_INFO("set DumpConfig in aclInit");
        ret = acl::AclDump::GetInstance().HandleDumpConfig(configPath);
        if (ret != ACL_SUCCESS) {
            ACL_LOG_INNER_ERROR("[Process][DumpConfig]process HandleDumpConfig failed");
            return ret;
        }
        ACL_LOG_INFO("set HandleDumpConfig success in aclInit");

        // init acl_op_executor
        ret = acl::InitCallbackManager::GetInstance().NotifyInitCallback(ACL_REG_TYPE_ACL_OP_EXECUTOR,
                                                                         cfgStr, cfgLen);
        if (ret != ACL_SUCCESS) {
            ACL_LOG_INNER_ERROR("call acl_op_executor init callback failed, ret:%d", ret);
            return ret;
        }

        ACL_LOG_INFO("set HandleDefaultDeviceAndStackSize in aclInit");
        // parse configPath for defaultDevice and call rtSetDefaultDeviced; parse device limit
        ret = acl::HandleDefaultDeviceAndStackSize(configPath);
        if (ret != ACL_SUCCESS) {
            ACL_LOG_INNER_ERROR("[Process][DefaultDevice]process HandleDefaultDevice failed");
            return ret;
        }
        ACL_LOG_INFO("set HandleDefaultDeviceAndStackSize success in aclInit");
    }
    const auto profRet = MsprofRegisterCallback(ASCENDCL, &acl::AclProfCtrlHandle);
    if (profRet != 0) {
        ACL_LOG_WARN("can not register Callback, prof result = %d", profRet);
    }

    // config profiling
    ACL_LOG_INFO("set ProfilingConfig in aclInit");
    ret = acl::AclProfiling::HandleProfilingConfig(configPath);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("[Process][ProfConfig]process HandleProfilingConfig failed");
        return ret;
    }

    // get socVersion
    const char *socName = aclrtGetSocNameImpl();
    if (socName == nullptr) {
        ACL_LOG_INNER_ERROR("[Init][Version]init soc version failed, ret = %d", ACL_ERROR_INTERNAL_ERROR);
        return ACL_ERROR_INTERNAL_ERROR;
    }

    // init acl dvpp
    ret = acl::InitCallbackManager::GetInstance().NotifyInitCallback(ACL_REG_TYPE_ACL_DVPP,
                                                                     cfgStr, cfgLen);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("call acl_dvpp init callback failed, ret:%d", ret);
        return ret;
    }

    // register kernel launch fill function
    if (acl::IsEnableAutoUCMemeory()) {
        ACL_LOG_INFO("register kernel launch fill function in aclInit");
        auto rtRegErr = rtRegKernelLaunchFillFunc("g_opSystemRunCfg", acl::UpdateOpSystemRunCfg);
        if (rtRegErr != RT_ERROR_NONE) {
            if (rtRegErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
                ACL_LOG_WARN("can not register kernel launch fill function, feature not support.");
            } else {
                ACL_LOG_INNER_ERROR("[Init][RegFillFunc]register failed,ret = %d.", rtRegErr);
                return ACL_GET_ERRCODE_RTS(rtRegErr);
            }
        }
    }

    ret = acl::InitCallbackManager::GetInstance().NotifyInitCallback(ACL_REG_TYPE_OTHER, cfgStr, cfgLen);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_ERROR("[Init][NotifyCallback]notify other init callback failed, ret = %d", ret);
        return ret;
    }

    acl::GetAllPackageVersion();

    aclFinalizeFlag = false;
    aclInitRefCount = 1;
    // 如果 aclJsonHash 为空，说明是第一次调用，设置aclJsonHash
    if (aclInitJsonHash.empty()) {
        aclInitJsonHash = currentHash;
        if (configPath != nullptr) {
            aclInitJsonPath = configPath;
        }
    }
    ACL_LOG_INFO("successfully execute aclInit, aclInitRefCount is %lu",aclInitRefCount);
    return ACL_SUCCESS;
}

aclError aclFinalizeInternal()
{
    ACL_LOG_INFO("start to execute aclFinalizeInternal");

    if (DlogReportFinalize() != 0) {
        ACL_LOG_WARN("can not init device's log module");
    }
    acl::ResourceStatistics::GetInstance().TraverseStatistics();
    const int32_t profRet = MsprofFinalize();
    if (profRet != MSPROF_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("[Finalize][Profiling]failed to call MsprofFinalize, prof errorCode = %d", profRet);
    }

    auto ret = acl::InitCallbackManager::GetInstance().NotifyFinalizeCallback(ACL_REG_TYPE_ACL_OP_COMPILER);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("call acl_op_compiler finalize callback failed, ret:%d", ret);
        return ret;
    }

    ret = acl::InitCallbackManager::GetInstance().NotifyFinalizeCallback(ACL_REG_TYPE_ACL_MODEL);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("call acl_model finalize callback failed, ret:%d", ret);
        return ret;
    }

    if (acl::AclDump::GetInstance().GetAdxInitFromAclInitFlag()) {
        const int32_t adxRet = AdxDataDumpServerUnInit();
        if (adxRet != 0) {
            ACL_LOG_CALL_ERROR("[Generate][DumpFile]generate dump file failed in disk, adx errorCode = %d", adxRet);
            return ACL_ERROR_INTERNAL_ERROR;
        }
    }

    // finalize acl dvpp
    ret = acl::InitCallbackManager::GetInstance().NotifyFinalizeCallback(ACL_REG_TYPE_ACL_DVPP);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("call acl_dvpp finalize callback failed, ret:%d", ret);
        return ret;
    }

    if (acl::IsEnableAutoUCMemeory()) {
        // unregister kernel launch fill function
        ACL_LOG_INFO("unregister kernel launch fill function in aclFinalize");
        auto rtRegErr = rtUnRegKernelLaunchFillFunc("g_opSystemRunCfg");
        if (rtRegErr != RT_ERROR_NONE) {
            if (rtRegErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
                ACL_LOG_WARN("can not unregister kernel launch fill function, feature not support.");
            } else {
                ACL_LOG_INNER_ERROR("Finalize][UnRegFillFunc]unregister failed,ret = %d.", rtRegErr);
                return ACL_GET_ERRCODE_RTS(rtRegErr);
            }
        }
    }

    // disable default device
    if (isEnableDefaultDevice) {
        ACL_LOG_INFO("disable default device");
        const auto rtErr = rtSetDefaultDeviceId(ACL_DEFAULT_DEVICE_DISABLE);
        if (rtErr != RT_ERROR_NONE) {
            ACL_LOG_WARN("close default device failed, ret:%d", rtErr);
            return ACL_GET_ERRCODE_RTS(rtErr);
        }
    }

    ret = acl::InitCallbackManager::GetInstance().NotifyFinalizeCallback(ACL_REG_TYPE_OTHER);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_ERROR("[Init][NotifyCallback]notify other finalize callback failed, ret = %d", ret);
        return ret;
    }

    aclFinalizeFlag = true;
    auto &aclInitRefCount = acl::GetAclInitRefCount();
    aclInitRefCount = 0;
    ACL_LOG_INFO("execute aclFinalizeInternal successfully");
    return ACL_SUCCESS;
}

aclError aclFinalize()
{
    ACL_STAGES_REG(acl::ACL_STAGE_FINAL, acl::ACL_STAGE_DEFAULT);
    ACL_LOG_INFO("start to execute aclFinalize");
    const std::unique_lock<std::recursive_mutex> lk(acl::GetAclInitMutex());
    if (aclFinalizeFlag) {
        ACL_LOG_INNER_ERROR("[Finalize][Acl]repeatedly finalized");
        return ACL_ERROR_REPEAT_FINALIZE;
    }

    const aclError ret = aclFinalizeInternal();
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("[Finalize][Acl]finalize internal failed, errorCode = %d", ret);
        return ret;
    }
    ACL_LOG_INFO("successfully execute aclFinalize");
    return ACL_SUCCESS;
}

aclError aclFinalizeReference(uint64_t *refCount)
{
    ACL_STAGES_REG(acl::ACL_STAGE_FINAL, acl::ACL_STAGE_DEFAULT);
    ACL_LOG_INFO("start to execute aclFinalizeReference");
    const std::unique_lock<std::recursive_mutex> lk(acl::GetAclInitMutex());
    auto &aclInitRefCount = acl::GetAclInitRefCount();
    if (refCount != nullptr) {
        *refCount = aclInitRefCount;
    }
    // 如果计数器大于1，则减少计数器并返回
    if (aclInitRefCount > 1) {
        aclInitRefCount--;
        if (refCount != nullptr) {
            *refCount = aclInitRefCount;
        }
        ACL_LOG_INFO("Found multiple acl references, reducing aclInitRefCount by 1. New aclInitRefCount: %lu", aclInitRefCount);
        return ACL_SUCCESS;
    }
    // 如果计数器小于1，报错
    if (aclInitRefCount < 1) {
        ACL_LOG_INNER_ERROR("[Finalize][Acl]aclFinalizeReference called repeatedly or called without proper aclInit");
        return ACL_ERROR_REPEAT_FINALIZE;
    }

    // 如果计数器等于1，则执行实际的资源清理操作
    const aclError ret = aclFinalizeInternal();
    if (refCount != nullptr) {
        *refCount = aclInitRefCount;
    }
    if (ret != ACL_SUCCESS) {
        ACL_LOG_INNER_ERROR("[Finalize][Acl]finalize internal failed, errorCode = %d", ret);
        return ret;
    }
    ACL_LOG_INFO("successfully execute aclFinalizeReference");
    return ACL_SUCCESS;
}

bool IsFileExist(const std::string &path)
{
    char_t realPath[MMPA_MAX_PATH] = {};
    return mmRealPath(path.c_str(), realPath, MMPA_MAX_PATH) == EN_OK;
}

bool ParseVersionInfo(const std::string &path, std::string &versionInfo)
{
    std::ifstream ifs(path, std::ifstream::in);
    if (!ifs.is_open()) {
        ACL_LOG_ERROR("[Check]Open file [%s] failed, reason is [%s].", path.c_str(), strerror(errno));
        return false;
    }

    std::string line;
    std::string lineVersion;
    std::string lineVersionDir;
    while (std::getline(ifs, line)) {
        const auto &pos = line.find(kVersionInfoKey);
        if (pos != std::string::npos) {
            ACL_LOG_DEBUG("Parse version success, content is [%s].", line.c_str());
            lineVersion = line.substr(pos + strlen(kVersionInfoKey));
        } else {
            const auto &posDir = line.find(kVersionDirInfoKey);
            if (posDir != std::string::npos) {
                ACL_LOG_DEBUG("Parse version_dir success, content is [%s].", line.c_str());
                lineVersionDir = line.substr(posDir + strlen(kVersionDirInfoKey));
                const auto &posPrefix = lineVersionDir.find(kVersionDirPrefix);
                if (posPrefix != std::string::npos) {
                    lineVersionDir = lineVersionDir.substr(posPrefix + strlen(kVersionDirPrefix));
                }
            }
        }

        if (!lineVersionDir.empty()) {
            ifs.close();
            versionInfo = lineVersionDir;
            return true;
        }
    }
    ifs.close();

    if (!lineVersion.empty()) {
        versionInfo = lineVersion;
        return true;
    }
    return false;
}

bool FillinPackageVersion(const std::string &versionInfo, aclCANNPackageVersion &version)
{
    (void)memset_s(&version, sizeof(aclCANNPackageVersion), 0, sizeof(aclCANNPackageVersion));
    std::vector<std::string> parts;
    acl::StringUtils::Split(versionInfo, '.', parts);
    constexpr uint32_t pkgVersionPartsMinCount = 2;
    constexpr uint32_t pkgVersionPartsMaxCount = 4;

    if (parts.size() < pkgVersionPartsMinCount) {
        return false;
    }

    while (parts.size() < pkgVersionPartsMaxCount) {
        parts.push_back("0");
    }

    if ((versionInfo.copy(version.version, ACL_PKG_VERSION_MAX_SIZE - 1) > 0) &&
        (parts[0].copy(version.majorVersion, ACL_PKG_VERSION_PARTS_MAX_SIZE - 1) > 0) &&
        (parts[1].copy(version.minorVersion, ACL_PKG_VERSION_PARTS_MAX_SIZE - 1) > 0) &&
        (parts[2].copy(version.releaseVersion, ACL_PKG_VERSION_PARTS_MAX_SIZE - 1) > 0) &&
        (parts[3].copy(version.patchVersion, ACL_PKG_VERSION_PARTS_MAX_SIZE - 1) > 0)) {
        return true;
    }

    return false;
}

bool GetDriverPath(const std::string &ascendInstallPath, std::string &driverPath)
{
    if (!IsFileExist(ascendInstallPath)) {
        ACL_LOG_WARN("[Check]ascendInstallPath [%s] does not exist.", ascendInstallPath.c_str());
        return false;
    }

    std::ifstream ifs(ascendInstallPath, std::ifstream::in);
    if (!ifs.is_open()) {
        ACL_LOG_ERROR("[Check]Open file [%s] failed, reason is [%s].", ascendInstallPath.c_str(), strerror(errno));
        return false;
    }

    driverPath.clear();
    std::string line;
    while (std::getline(ifs, line)) {
        const auto &pos = line.find(kDriverPathKey);
        if (pos == std::string::npos) {
            continue;
        }
        ACL_LOG_DEBUG("Parse driver path success, content is [%s].", line.c_str());
        driverPath = line.substr(pos + strlen(kDriverPathKey));
        if (!driverPath.empty()) {
            ifs.close();
            ACL_LOG_INFO("driver path is [%s].", driverPath.c_str());
            return true;
        }
    }
    ifs.close();
    return false;
}

aclError GetCANNVersionInternal(const aclCANNPackageName name, aclCANNPackageVersion &version,
    const std::string &installPath)
{
    std::string versionInfoPath = installPath + "/" + kMapToPkgName.at(name) + "/version.info";
    if (!IsFileExist(versionInfoPath)) {
        ACL_LOG_WARN("[Check]versionInfoPath [%s] does not exist.", versionInfoPath.c_str());
        return ACL_ERROR_INVALID_FILE;
    }

    std::string versionInfo;
    if (!ParseVersionInfo(versionInfoPath, versionInfo)) {
        ACL_LOG_ERROR("[Check]failed to parse versionInfo, the versionInfoPath is [%s].",
            versionInfoPath.c_str());
        return ACL_ERROR_INVALID_FILE;
    }

    ACL_LOG_INFO("versionInfo is [%s].", versionInfo.c_str());
    if (!FillinPackageVersion(versionInfo, version)) {
        ACL_LOG_ERROR("[Check]failed to run FillinPackageVersion.");
        return ACL_ERROR_INVALID_FILE;
    }

    return ACL_SUCCESS;
}

aclError aclsysGetCANNVersion(aclCANNPackageName name, aclCANNPackageVersion *version)
{
    ACL_LOG_INFO("start to execute aclsysGetCANNVersion.");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(version);
    ACL_LOG_INFO("enum name id is [%d], enum name is [%s].", (int32_t)name,
        kMapToPkgName.count(name) ? kMapToPkgName.at(name).c_str() : "unknown" );

    char *pathEnv = nullptr;
    std::string driverPath;
    aclError ret = ACL_SUCCESS;
    switch (name) {
        case ACL_PKG_NAME_CANN:
        case ACL_PKG_NAME_RUNTIME:
        case ACL_PKG_NAME_COMPILER:
        case ACL_PKG_NAME_HCCL:
        case ACL_PKG_NAME_TOOLKIT:
        case ACL_PKG_NAME_OPP:
        case ACL_PKG_NAME_OPP_KERNEL:
            MM_SYS_GET_ENV(MM_ENV_ASCEND_HOME_PATH, pathEnv);
            if (!pathEnv) {
                ACL_LOG_WARN("[Check]can not get env [%s].", kAscendHomeEnvName);
                ret = ACL_ERROR_INVALID_FILE;
                break;
            }
            ACL_LOG_INFO("value of env [%s] is [%s].", kAscendHomeEnvName, pathEnv);
            ret = GetCANNVersionInternal(name, *version, std::string(pathEnv));
            break;
        case ACL_PKG_NAME_DRIVER:
            if (!GetDriverPath(kAscendInstallPath, driverPath)) {
                ret = ACL_ERROR_INVALID_FILE;
                break;
            }
            ret = GetCANNVersionInternal(name, *version, driverPath);
            break;
        default:
            ACL_LOG_ERROR("[Check]package name enum id [%d] is invalid.", (int32_t)name);
            ret = ACL_ERROR_INVALID_PARAM;
            break;
    }
    return ret;
}

static std::string GetFaultEventInfo()
{
    std::string faultInfo;

    int32_t deviceId = 0;
    auto ret = rtGetDevice(&deviceId);
    if (ret != RT_ERROR_NONE) {
        ACL_LOG_INFO("can not get device id, runtime errorCode is %d", static_cast<int32_t>(ret));
        return faultInfo;
    }

    rtDmsEventFilter filter = {};
    const uint32_t maxFaultNum = 128UL; // max is 128
    rtDmsFaultEvent faultEventInfo[maxFaultNum] = {};
    uint32_t eventCount = 0UL;
    ret = rtGetFaultEvent(deviceId, &filter, faultEventInfo, maxFaultNum, &eventCount);
    if (ret != RT_ERROR_NONE || eventCount == 0UL) {
        ACL_LOG_INFO("can not get fault event of device %d, runtime errorCode is %d",
            deviceId, static_cast<int32_t>(ret));
        return faultInfo;
    }

    for (uint32_t faultIndex = 0; faultIndex < eventCount; ++faultIndex) {
        std::ostringstream oss;
        oss << std::hex << faultEventInfo[faultIndex].eventId;
        faultInfo = faultInfo + "[0x" + oss.str() + "]"
            + faultEventInfo[faultIndex].eventName + ";";
    }

    if (faultInfo.empty()) {
        return faultInfo;
    }
    faultInfo = "Fault diagnosis analysis: " + faultInfo;
    return faultInfo;
}

const char *aclGetRecentErrMsg()
{
    ACL_LOG_INFO("start to execute aclGetRecentErrMsg.");
    constexpr const rtGetDevMsgType_t msgType = RT_GET_DEV_ERROR_MSG;
    const auto ret = rtGetDevMsg(msgType, &acl::aclGetMsgCallback);
    if (ret != RT_ERROR_NONE) {
        ACL_LOG_DEBUG("can not get device errorMessage, runtime errorCode is %d",
            static_cast<int32_t>(ret));
    }

    const std::string faultEventMsg = GetFaultEventInfo();
    if (!faultEventMsg.empty()) {
        if (aclRecentErrMsg.empty()) {
            aclRecentErrMsg = faultEventMsg;
        } else {
            aclRecentErrMsg = aclRecentErrMsg + "\n" + faultEventMsg;
        }
    }

    const std::string aclHostErrMsg = ErrorManager::GetInstance().GetErrorMessage();
    if ((aclHostErrMsg.empty()) && (aclRecentErrMsg.empty())) {
        ACL_LOG_DEBUG("get errorMessage is empty");
        return nullptr;
    }

    if (aclHostErrMsg.empty()) {
        return aclRecentErrMsg.c_str();
    }

    if (aclRecentErrMsg.empty()) {
        (void)aclRecentErrMsg.assign(aclHostErrMsg);
        return aclRecentErrMsg.c_str();
    }

    aclRecentErrMsg = aclHostErrMsg + "\n" + aclRecentErrMsg;
    ACL_LOG_INFO("execute aclGetRecentErrMsg successfully.");
    return aclRecentErrMsg.c_str();
}

aclError aclGetCannAttributeList(const aclCannAttr **cannAttrList, size_t *num)
{
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(cannAttrList);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(num);
    const aclError ret = acl::CannInfoUtils::GetAttributeList(cannAttrList, num);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_ERROR("failed to get attrList, ret = %d", ret);
        return ret;
    }
    ACL_LOG_INFO("execute aclGetCannAttributeList successfully.");
    return ACL_SUCCESS;
}

aclError aclGetCannAttribute(aclCannAttr cannAttr, int32_t *value)
{
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(value);
    const aclError ret = acl::CannInfoUtils::GetAttribute(cannAttr, value);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_ERROR("failed to check, attr value = %d, ret = %d", static_cast<int32_t>(cannAttr), ret);
        return ret;
    }
    ACL_LOG_INFO("execute aclGetCannAttribute successfully.");
    return ACL_SUCCESS;
}

aclError aclGetDeviceCapability(uint32_t deviceId, aclDeviceInfo deviceInfo, int64_t *value)
{
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(value);
    int32_t count = -1;
    const rtError_t rtErr = rtGetDeviceCount(&count);
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("get device count failed, runtime result = %d.", static_cast<int32_t>(rtErr));
        return ACL_GET_ERRCODE_RTS(rtErr);
    }
    // currently check only, deviceId with [0, count - 1]
    ACL_CHECK_LESS_UINT(deviceId, static_cast<uint32_t>(count - 1));
    const std::map<aclDeviceInfo, std::string> infoTypeToKey = {
        {ACL_DEVICE_INFO_AI_CORE_NUM, "ai_core_cnt"},
        {ACL_DEVICE_INFO_VECTOR_CORE_NUM, "vector_core_cnt"},
        {ACL_DEVICE_INFO_L2_SIZE, "l2_size"}
    };
    const auto iter = infoTypeToKey.find(deviceInfo);
    if (iter == infoTypeToKey.end()) {
        ACL_LOG_WARN("get device info failed, invalid info type = %d", static_cast<int32_t>(deviceInfo));
        return ACL_ERROR_INVALID_PARAM;
    }
    const auto &key = iter->second;
    const aclError ret = GetPlatformInfoWithKey(key, value);
    if (ret != ACL_SUCCESS) {
        ACL_LOG_ERROR("get device info failed, info type = %d, key = %s", static_cast<int32_t>(deviceInfo), key.c_str());
        return ret;
    }
    ACL_LOG_INFO("execute aclGetDeviceCapability successfully.");
    return ACL_SUCCESS;
}
